//
//  ZSVisitView.swift
//  ZSWeibo
//
//  Created by zsj1992 on 16/4/6.
//  Copyright © 2016年 ichange. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol ZSVisitViewDelegate : NSObjectProtocol{
   optional func btnLoginDidClick()
    func btnRegisterDidClick()

}


/// 访客模式下的界面
class ZSVisitView: UIView {

   weak var delegate : ZSVisitViewDelegate?
    
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let value:CGFloat = 237/255.0
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
        addSubview(circleIcon)
        addSubview(backGroundImage)

        addSubview(lblTips)
        addSubview(btnLogin)
        addSubview(btnRegister)
        addSubview(largeHouse)
        
        setUpUI()
        
}

    func setUpUI(){
        
        //make相当于需要进行约束的控件
            circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)

        
                
            backGroundImage.snp_makeConstraints(closure: { (make) -> Void in
                make.center.equalTo(circleIcon)
            })
                
            //核心动画的代码
            let anim = CABasicAnimation(keyPath: "transform.rotation")
            anim.fromValue = 0
            anim.toValue = M_PI*2
            anim.removedOnCompletion = false
            anim.repeatCount = MAXFLOAT
            anim.duration = 8.0
            circleIcon.layer.addAnimation(anim, forKey: nil)

                
        }

        lblTips.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            make.centerX.equalTo(self.snp_centerX)
            make.width.equalTo(230)
            make.height.equalTo(50)
        }
        
        btnLogin.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(lblTips.snp_left)

            make.top.equalTo(lblTips.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        btnRegister.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(lblTips.snp_right)
            
            make.top.equalTo(lblTips.snp_bottom).offset(16)
            make.width.equalTo(100)
        }

        largeHouse.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //访客视图暴露一个对象方法
    func setVisitorViewInfo(imageName:String,text:String,isHome:Bool=false){

        circleIcon.hidden = !isHome
        
        largeHouse.image = UIImage(named: imageName)
        
        lblTips.text = text
    
    }

    /**
    *  懒加载所有的子控件
    */
    //
    //把子视图 懒加载出来
    //圈圈  大小和图片的大小一致 没有 origin
    
    private lazy var circleIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //大房子
    private lazy var largeHouse = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    
    
    private lazy var backGroundImage = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
 
    //提示文案
    
    private lazy var lblTips:UILabel={
        let lbltip = UILabel()
        lbltip.text = "关注一些人,回到这里看看有什么惊喜"
        lbltip.numberOfLines = 0
        lbltip.font = UIFont.systemFontOfSize(14)
        lbltip.textAlignment = .Center
//        lbltip.preferredMaxLayoutWidth = 255
        lbltip.textColor = UIColor.darkGrayColor()
        return lbltip
        }()
    
    

    //登录 注册按钮

    private lazy var btnLogin : UIButton = {
    let btnLogin = UIButton()
    btnLogin.setTitle("登录", forState: .Normal)
        
    btnLogin.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:.Normal)
    btnLogin.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
    btnLogin.titleLabel?.font = UIFont.systemFontOfSize(14)
        
    btnLogin.addTarget(self, action: "btnLoginClick", forControlEvents: .TouchUpInside)
        
    return btnLogin
    }()
    
    private lazy var btnRegister : UIButton = {
        let btnRegister = UIButton()
        btnRegister.setTitle("注册", forState: .Normal)
        btnRegister.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btnRegister.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:.Normal)
        btnRegister.titleLabel?.font = UIFont.systemFontOfSize(14)
        btnRegister.addTarget(self, action: "btnRegisterClick", forControlEvents: .TouchUpInside)

        
        return btnRegister
    }()

    
    //在视图点击了登录按钮
    func btnLoginClick(){
    delegate?.btnLoginDidClick?()
        
    }
    //在视图点击了注册按钮
    func btnRegisterClick(){
    
        delegate?.btnRegisterDidClick()
    
    }
    
    
    
    
    
}
